﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DSS_Simplex
{
    public delegate void SearchContextChangedHandler(List<field> fields);

    public struct field
    {
        public string Field;
        public string FriendlyName;
        public string Value;
    }

    class Mesin
    {
        //for reporting
        public static List<double[,]> ListRepValue;
        //public static List<int> ListRepIndex;

        public static string[] ArrBasisVar;
        public static string[] ArrNonBasisVar;
        public static List<string[]> ListBasisVar;
        public static int VarSlack, VarSurplus, VarArtfS, VarArtf;
        public static double[] CoefObj;

        public static void CariMax(double[,] table)
        {
            int i, j = 0, s, n;
            double calc;
            int baris, kolom;
            double tanda;
            double min, max;
            int flag = 0;

            baris = table.GetLength(0);
            kolom = table.GetLength(1);
            double[] tanda2 = new double[baris - 1];

            //buat list report
            //ListRepIndex = new List<int>();
            ListRepValue = new List<double[,]>();
            ListBasisVar = new List<string[]>();

            ListRepValue.Add(new double[table.GetLength(0), table.GetLength(1)]);
            Array.Copy(table, ListRepValue.ElementAt(ListRepValue.Count() - 1), table.Length);

            ListBasisVar.Add(new string[ArrBasisVar.GetLength(0)]);
            Array.Copy(ArrBasisVar, ListBasisVar.ElementAt(ListBasisVar.Count() - 1), ArrBasisVar.Length);

            for (i = 1; i < kolom; i++)
            {
                if (table[0, i] >= 0)
                    flag = 0;
                if (table[0, i] < 0)
                {
                    flag = 1;
                    break;
                }
            }
            Console.WriteLine("nilai flag:::: " + flag);
            while (flag == 1)
            {
                max = table[0, 1];
                s = 1;

                Console.WriteLine("%%%%%%%%%%%%%%%%%%%");

                for (i = 1; i < kolom; i++)
                {
                    if (max > table[0, i])
                    {
                        max = table[0, i];
                        s = i;
                    }
                }
                Console.Write("titik maksimum: ");
                Console.WriteLine(max);
                min = table[1, 0] / table[1, s];
                j = 1;

                for (i = 2; i < baris; i++)
                    if (table[i, s] != 0)
                    {
                        tanda2[i - 1] = table[i, 0] / table[i, s];

                        if (tanda2[i - 1] > 0 && ((tanda2[i - 1] < min) || (min <= 0)))
                        {
                            min = tanda2[i - 1];
                            j = i;
                        }

                    }
                Console.Write("titik rasio: ");
                Console.WriteLine(min);
                tanda = table[j, s];
                Console.Write("tanda: ");
                Console.WriteLine(tanda);
                ArrBasisVar[j - 1] = ArrNonBasisVar[s - 1];

                for (i = 0; i < kolom; i++)
                {
                    table[j, i] /= tanda;
                }

                for (i = 0; i < baris; i++)
                {
                    if (i != j)
                    {
                        calc = table[i, s] / table[j, s];
                        for (n = 0; n < kolom; n++)
                        {
                            table[i, n] -= table[j, n] * calc;
                        }
                    }
                }
                Console.WriteLine("iterasi:");
                for (i = 0; i < baris; i++)
                {
                    for (j = 0; j < kolom; j++)
                    {
                        Console.Write(table[i, j]);
                        Console.Write(" ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("end iteration.");

                ListBasisVar.Add(new string[ArrBasisVar.GetLength(0)]);
                Array.Copy(ArrBasisVar, ListBasisVar.ElementAt(ListBasisVar.Count() - 1), ArrBasisVar.Length);
                //add new table into list
                Console.WriteLine("sebelummmmmmmmmmmmmmm " + ListRepValue.Count());
                //ListRepValue.Add(table);
                ListRepValue.Add(new double[table.GetLength(0), table.GetLength(1)]);
                Array.Copy(table, ListRepValue.ElementAt(ListRepValue.Count() - 1), table.Length);
                Console.WriteLine("sudahhhhhhhhhhhhhhhhh " + ListRepValue.Count());

                Console.WriteLine("ini list : ");

                for (int ii = 0; ii < ListRepValue.ElementAt(0).GetLength(0); ii++)
                {
                    for (int jj = 0; jj < ListRepValue.ElementAt(0).GetLength(1); jj++)
                    {
                        Console.Write("" + ListRepValue.ElementAt(0)[ii, jj]);

                    }
                }
                Console.WriteLine("iterasi:");
                for (i = 0; i < baris; i++)
                {
                    for (j = 0; j < kolom; j++)
                    {
                        Console.Write(table[i, j]);
                        Console.Write(" ");
                    }
                    Console.WriteLine();
                }



                Console.WriteLine("end iteration.");

                for (i = 1; i < kolom; i++)
                {
                    if (table[0, i] >= 0)
                        flag = 0;
                    if (table[0, i] < 0)
                    {
                        flag = 1;
                        break;
                    }
                }
            }
            Console.WriteLine("cek semua list : ");

            for (int ii = 0; ii < ListRepValue.Count(); ii++)
            {
                for (int jj = 0; jj < ListRepValue.ElementAt(ii).GetLength(0); jj++)
                {
                    for (int kk = 0; kk < ListRepValue.ElementAt(ii).GetLength(1); kk++)
                        Console.Write(" " + ListRepValue.ElementAt(ii)[jj, kk]);
                    Console.WriteLine(" -------------------------------------------");
                }
                Console.WriteLine();
            }

        }

        public static void CariMin(double[,] table)
        {
            int i, j = 0, s, n;
            double calc;
            int baris, kolom;
            double tanda;
            double min, max;
            int flag = 0;

            baris = table.GetLength(0);
            kolom = table.GetLength(1);
            double[] tanda2 = new double[baris - 1];

            //buat list
            ListRepValue = new List<double[,]>();
            ListBasisVar = new List<string[]>();
            //list ke 0
            ListRepValue.Add(new double[table.GetLength(0), table.GetLength(1)]);
            Array.Copy(table, ListRepValue.ElementAt(ListRepValue.Count() - 1), table.Length);
            ListBasisVar.Add(new string[ArrBasisVar.GetLength(0)]);
            Array.Copy(ArrBasisVar, ListBasisVar.ElementAt(ListBasisVar.Count() - 1), ArrBasisVar.Length);

            for (i = 1; i < kolom; i++)
            {
                if (table[0, i] <= 0)
                    flag = 0;
                if (table[0, i] > 0)
                {
                    flag = 1;
                    break;
                }
            }
            Console.WriteLine("nilai flag:::: " + flag);
            while (flag == 1)
            {
                max = table[0, 1];
                s = 1;

                Console.WriteLine("%%%%%%%%%%%%%%%%%%%");

                for (i = 1; i < kolom; i++)
                {
                    if (max < table[0, i])
                    {
                        max = table[0, i];
                        s = i;
                    }
                }
                Console.Write("titik maksimum: ");
                Console.WriteLine(max);
                min = table[1, 0] / table[1, s];
                j = 1;

                for (i = 2; i < baris; i++)
                    if (table[i, s] != 0)
                    {
                        tanda2[i - 1] = table[i, 0] / table[i, s];

                        if (tanda2[i - 1] > 0 && ((tanda2[i - 1] < min) || (min <= 0)))
                        {
                            min = tanda2[i - 1];
                            j = i;
                        }

                    }
                Console.Write("titik rasio: ");
                Console.WriteLine(min);
                tanda = table[j, s];

                ArrBasisVar[j - 1] = ArrNonBasisVar[s - 1];

                for (i = 0; i < kolom; i++)
                {
                    table[j, i] /= tanda;
                }
                for (i = 0; i < baris; i++)
                {
                    if (i != j)
                    {
                        calc = table[i, s] / table[j, s];
                        for (n = 0; n < kolom; n++)
                        {
                            table[i, n] -= table[j, n] * calc;
                        }
                    }
                }
                Console.WriteLine("iterasi:");
                for (i = 0; i < baris; i++)
                {
                    for (j = 0; j < kolom; j++)
                    {
                        Console.Write(table[i, j]);
                        Console.Write(" ");
                    }
                    Console.WriteLine();
                }
                Console.WriteLine("end iteration.");
                ListBasisVar.Add(new string[ArrBasisVar.GetLength(0)]);
                Array.Copy(ArrBasisVar, ListBasisVar.ElementAt(ListBasisVar.Count() - 1), ArrBasisVar.Length);
                //add new table into list
                Console.WriteLine("sebelummmmmmmmmmmmmmm " + ListRepValue.Count());
                //ListRepValue.Add(table);
                ListRepValue.Add(new double[table.GetLength(0), table.GetLength(1)]);
                Array.Copy(table, ListRepValue.ElementAt(ListRepValue.Count() - 1), table.Length);
                Console.WriteLine("sudahhhhhhhhhhhhhhhhh " + ListRepValue.Count());
                Console.WriteLine("ini list : ");

                for (int ii = 0; ii < ListRepValue.ElementAt(ListRepValue.Count() - 1).GetLength(0); ii++)
                {
                    for (int jj = 0; jj < ListRepValue.ElementAt(ListRepValue.Count() - 1).GetLength(1); jj++)
                    {
                        Console.Write("" + ListRepValue.ElementAt(ListRepValue.Count() - 1)[ii, jj]);

                    }
                }
                Console.WriteLine("iterasi:");

                for (i = 1; i < kolom; i++)
                {
                    if (table[0, i] <= 0)
                        flag = 0;
                    if (table[0, i] > 0)
                    {
                        flag = 1;
                        break;
                    }
                }
            }

            Console.WriteLine("cek semua list : ");

            for (int ii = 0; ii < ListRepValue.Count(); ii++)
            {
                for (int jj = 0; jj < ListRepValue.ElementAt(ii).GetLength(0); jj++)
                {
                    for (int kk = 0; kk < ListRepValue.ElementAt(ii).GetLength(1); kk++)
                        Console.Write(" " + ListRepValue.ElementAt(ii)[jj, kk]);
                    Console.WriteLine(" -------------------------------------------");
                }
                Console.WriteLine();
            }

            for (int ii = 0; ii < ListRepValue.ElementAt(ListRepValue.Count() - 1).GetLength(0); ii++)
            {
                for (int jj = 0; jj < ListRepValue.ElementAt(ListRepValue.Count() - 1).GetLength(1); jj++)
                {
                    Console.Write("" + ListRepValue.ElementAt(ListRepValue.Count() - 1)[ii, jj]);

                }
            }
        }

        public static void InnerProduct(ref double[,] Mtable, int A1, int S2, int A2, double Mval)
        {
            int i, j;
            int VarAll = S2 + A2 + A1;
            int TCol = Mtable.GetLength(1);
            double[] VectorA = new double[VarAll];
            double[] coefVar = new double[TCol];
            double sum = 0;

            for (i = 0; i < S2; i++)
            {
                VectorA[i] = 0;
            }
            for (i = S2; i < S2 + A1; i++)
            {
                VectorA[i] = Mval;
            }
            for (i = S2 + A1; i < VarAll; i++)
            {
                VectorA[i] = Mval;
            }
            //sum product
            for (j = 0; j < TCol; j++)
            {
                for (i = 0; i < VectorA.Length; i++)
                {
                    sum += VectorA[i] * Mtable[i + 1, j];

                }
                coefVar[j] = sum - Mtable[0, j];
                sum = 0;

            }

            //ganti dgn var baru
            for (i = 0; i < TCol; i++)
            {
                Mtable[0, i] = coefVar[i];
            }

        }

        public static string[] ArrRep;
        public static string[] ArrRepHeader;

        public static void RunComputation(bool toGetMax, DataGridView dataGridView1, DataGridView dataGridView2, DataGridView dataGridView4)
        {
            int i;
            int j = 0;
            //from DG1
            int DG1max = 0;
            int DG1min = 0;
            int DG1eq = 0;
            int DG1add = 0;

            //constraint from DG1
            List<int> listDG1max = new List<int>();
            List<int> listDG1min = new List<int>();
            List<int> listDG1eq = new List<int>();
            List<int> listDG1add = new List<int>();
            //from DG2
            List<string> ListNonBasisVar = new List<string>();
            CoefObj = new double[dataGridView1.RowCount - 1];

            //simpan data dari DGV1 min and max
            for (i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                CoefObj[i] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");

                if (Convert.ToString(dataGridView1.Rows[i].Cells[3].Value) != string.Empty && Convert.ToString(dataGridView1.Rows[i].Cells[2].Value) == string.Empty)
                {
                    DG1max++;
                    listDG1max.Add(i);
                }
                else if (Convert.ToString(dataGridView1.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView1.Rows[i].Cells[3].Value) == string.Empty)
                {
                    DG1min++;
                    listDG1min.Add(i);
                }
                else if (Convert.ToString(dataGridView1.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView1.Rows[i].Cells[3].Value) != string.Empty && Convert.ToString(dataGridView1.Rows[i].Cells[2].Value) == Convert.ToString(dataGridView1.Rows[i].Cells[3].Value))
                {
                    DG1eq++;
                    listDG1eq.Add(i);
                }
                else if (Convert.ToString(dataGridView1.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView1.Rows[i].Cells[3].Value) != string.Empty && Convert.ToString(dataGridView1.Rows[i].Cells[2].Value) != Convert.ToString(dataGridView1.Rows[i].Cells[3].Value))
                {
                    DG1add++;
                    listDG1add.Add(i);
                }
            }


            List<int> listDG2max = new List<int>();
            List<int> listDG2min = new List<int>();
            List<int> listDG2eq = new List<int>();
            List<int> listDG2add = new List<int>();

            //isi list dgn nilai DGview2
            //nilai max,min,eq,add dr DG1 ada di listDG1
            for (i = 0; i < dataGridView2.RowCount - 1; i++)
            {

                if (Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) == string.Empty)
                {
                    listDG2max.Add(i);
                }
                if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) == string.Empty)
                {
                    listDG2min.Add(i);
                }
                if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) == Convert.ToString(dataGridView2.Rows[i].Cells[2].Value))
                {
                    listDG2eq.Add(i);
                }
                if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != Convert.ToString(dataGridView2.Rows[i].Cells[2].Value))
                {
                    listDG2add.Add(i);
                }
            }


            int varinit = dataGridView1.RowCount - 1;
            int constraint = dataGridView2.RowCount - 1;
            int vartotal = varinit + constraint;

            //define varinit,const,vartotal for all
            //simplexMax
            int varinit_SMax = varinit;
            int constraint_SMax = constraint + DG1max;
            int vartotal_SMax = vartotal + DG1max;
            //simplexMin
            int varinit_SMin = varinit;
            int constraint_SMin = constraint + DG1min;
            int vartotal_SMin = vartotal + DG1min;
            //simplexBigM

            //simplex
            double[,] coef = new double[constraint_SMax, vartotal_SMax];
            double[,] bcoef = new double[constraint_SMax, 1];
            double[] ccoef = new double[vartotal_SMax];
            double[,] table = new double[constraint_SMax + 1, vartotal_SMax + 1];

            //dual
            double[,] coefd = new double[constraint_SMin, varinit];
            double[,] tcoef = new double[varinit, vartotal_SMin];
            double[,] tbcoef = new double[varinit, 1];
            double[] tccoef = new double[vartotal_SMin];
            double[,] ttable = new double[varinit + 1, vartotal_SMin + 1];
            //int tbrs;
            //int tklm;

            //bigM

            //hapus
            double[] arrMin = new double[constraint];
            double[] arrMax = new double[constraint];
            double[] arrEq = new double[constraint];
            double[] arrAddmin = new double[constraint];
            double[] arrAddmax = new double[constraint];


            VarSurplus = new int();
            VarSlack = new int();
            VarArtfS = new int();
            VarArtf = new int();

            #region kondisi

            #region arr report iteration
            //isi array utk report
            ArrRep = new string[constraint];
            ArrRepHeader = new string[varinit];

            for (i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                ArrRepHeader[i] = dataGridView1.Rows[i].Cells[0].Value + "";
            }

            #endregion

            //kondisi
            int simMin = 0, simMax = 0, simEq = 0;
            int countArr = 0;
            //cek batasan max dr DG2 & DG1
            for (i = 0; i < dataGridView2.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) == string.Empty)
                {
                    simMax++;
                    ArrRep[countArr] = dataGridView2.Rows[i].Cells[0].Value + "";
                    countArr++;
                }
            }
            for (i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView1.Rows[i].Cells[3].Value) != string.Empty && Convert.ToString(dataGridView1.Rows[i].Cells[2].Value) == string.Empty)
                {
                    simMax++;
                }
            }
            //cek batasan min
            for (i = 0; i < dataGridView2.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) == string.Empty)
                {
                    simMin++;
                    ArrRep[countArr] = dataGridView2.Rows[i].Cells[0].Value + "";
                    countArr++;
                }
            }
            for (i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView1.Rows[i].Cells[3].Value) == string.Empty && Convert.ToString(dataGridView1.Rows[i].Cells[2].Value) != string.Empty)
                {
                    simMin++;
                }
            }
            //cek batasan eq
            for (i = 0; i < dataGridView2.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView2.Rows[i].Cells[1].Value) == Convert.ToString(dataGridView2.Rows[i].Cells[2].Value))
                {
                    simEq++;
                    ArrRep[countArr] = dataGridView2.Rows[i].Cells[0].Value + "";
                    countArr++;
                }
            }
            for (i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                if (Convert.ToString(dataGridView1.Rows[i].Cells[3].Value) != string.Empty && Convert.ToString(dataGridView1.Rows[i].Cells[2].Value) != string.Empty && Convert.ToString(dataGridView1.Rows[i].Cells[3].Value) == Convert.ToString(dataGridView1.Rows[i].Cells[2].Value))
                {
                    simEq++;
                }
            }
            //cek jml nilai yg sama
            #endregion

            #region Button Maksimasi
            //kondisi maksimum
            if (toGetMax)
            {

                #region Simplex Max

                if (simMax == constraint_SMax && simMin == 0 && simEq == 0)
                {
                    //matriks awal
                    //dr GDV2
                    ArrBasisVar = new string[constraint_SMax];
                    ArrNonBasisVar = new string[vartotal_SMax];

                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coef[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" awal");
                    }
                    //dr GDV1
                    for (i = constraint; i < constraint_SMax; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coef[i, j] = 0;
                        }
                    }
                    int count2 = 0;
                    for (i = constraint; i < constraint_SMax; i++)
                    {
                        coef[i, listDG1max.ElementAt(count2)] = 1;
                        count2++;

                    }

                    //isi nol
                    for (i = 0; i < constraint_SMax; i++)
                    {
                        for (j = varinit; j < vartotal_SMax; j++)
                        {
                            coef[i, j] = 0;
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" tambahan variabel");
                    }
                    //buat variabel bantu
                    for (i = 0; i < constraint_SMax; i++)
                    {
                        for (j = varinit; j < vartotal_SMax; j++)
                        {
                            coef[i, varinit + i] = 1;
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" tambah 1");
                    }

                    //nilai kanan jika smua constraint memiliki batas maksimum
                    for (i = 0; i < constraint; i++)
                    {
                        bcoef[i, 0] = double.Parse(dataGridView2.Rows[i].Cells[2].Value + "");
                        //report
                        ArrBasisVar[i] = "Slack" + (1 + i + "");
                    }

                    int count = 0;
                    for (i = constraint; i < constraint_SMax; i++)
                    {
                        bcoef[i, 0] = double.Parse(dataGridView1.Rows[listDG1max.ElementAt(count)].Cells[3].Value + "");
                        count++;
                        //report
                        ArrBasisVar[i] = "Slack" + (1 + i + "");

                    }
                    VarSlack = ArrBasisVar.Count();
                    //koefisien fungsi tujuan
                    for (j = 0; j < varinit; j++)
                    {
                        ccoef[j] = double.Parse(dataGridView1.Rows[j].Cells[1].Value + "");
                        //report
                        ArrNonBasisVar[j] = dataGridView1.Rows[j].Cells[0].Value + "";
                    }
                    int counter = 1;
                    for (j = varinit; j < vartotal_SMax; j++)
                    {
                        ccoef[j] = 0;
                        //report
                        ArrNonBasisVar[j] = "Slack" + (counter + "");
                        counter++;
                    }
                    //menghasilkan tabel
                    table[0, 0] = 0;
                    for (i = 1; i < vartotal_SMax + 1; i++)
                    {
                        table[0, i] = ccoef[i - 1];
                    }
                    for (i = 1; i < constraint_SMax + 1; i++)
                    {
                        table[i, 0] = bcoef[i - 1, 0];
                    }
                    for (i = 1; i < constraint_SMax + 1; i++)
                    {
                        for (j = 1; j < vartotal_SMax + 1; j++)
                        {
                            table[i, j] = coef[i - 1, j - 1];
                        }
                    }
                    //matriks awal
                    for (i = 0; i < constraint_SMax + 1; i++)
                    {
                        for (j = 0; j < vartotal_SMax + 1; j++)
                        {
                            Console.Write(table[i, j]);
                        }
                        Console.WriteLine("tabel");
                    }
                    for (i = 0; i < ArrBasisVar.Count(); i++)
                    {
                        Console.Write(ArrBasisVar[i] + "    ");
                    }
                    Console.WriteLine("");
                    for (i = 0; i < ArrNonBasisVar.Count(); i++)
                    {
                        Console.Write(ArrNonBasisVar[i] + "    ");
                    }
                    Console.WriteLine("");
                    Mesin.CariMin(table);
                    //hasil
                    for (i = 0; i < constraint_SMax + 1; i++)
                    {
                        for (j = 0; j < vartotal_SMax + 1; j++)
                            Console.Write("{0,5}  ", table[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", -table[0, 0]);

                }
                #endregion

                #region Dual Max
                /*
                else if (simMin == constraint_SMin && simMax == 0 && simEq == 0)
                {
                    Console.WriteLine("d2");
                    //matriks awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coefd[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coefd[i, j]);

                        }
                        Console.WriteLine(" matrik awal");
                    }
                    for (i = constraint; i < constraint_SMin; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coefd[i, j] = 0;
                        }
                    }
                    int count3 = 0;
                    for (i = constraint; i < constraint_SMin; i++)
                    {
                        coefd[i, listDG1min.ElementAt(count3)] = 1;
                        count3++;

                    }

                    //transpose
                    tklm = coefd.GetLength(0);
                    Console.WriteLine(tklm);
                    tbrs = coefd.GetLength(1);
                    Console.WriteLine(tbrs);

                    //transpose matrikx
                    for (i = 0; i < tklm; i++)
                    {
                        for (j = 0; j < tbrs; j++)
                        {
                            tcoef[j, i] = coefd[i, j];
                        }
                    }
                    Console.WriteLine(" matriks transpose");

                    //tambah coef dg 0
                    for (i = 0; i < varinit; i++)
                        for (j = constraint_SMin; j < vartotal_SMin; j++)
                        {
                            tcoef[i, j] = 0;

                        }
                    //var s
                    for (i = 0; i < varinit; i++)
                    {
                        for (j = constraint_SMin; j < vartotal_SMin; j++)
                        {
                            tcoef[i, constraint_SMin + i] = 1;
                        }
                    }

                    //matriks kendala
                    for (i = 0; i < varinit; i++)
                    {
                        for (j = 0; j < vartotal_SMin; j++)
                        {
                            Console.Write(tcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    //nilai kanan diambil dr fungsi tujuan
                    for (i = 0; i < varinit; i++)
                    {
                        tbcoef[i, 0] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                    }

                    //koefisien fungsi tujuan
                    for (j = 0; j < constraint; j++)
                    {
                        tccoef[j] = double.Parse(dataGridView2.Rows[j].Cells[1].Value + "");
                    }
                    int count = 0;
                    for (j = constraint; j < constraint_SMin; j++)
                    {
                        tccoef[j] = double.Parse(dataGridView1.Rows[listDG1min.ElementAt(count)].Cells[2].Value + "");
                        count++;
                    }
                    for (j = constraint_SMin; j < vartotal_SMin; j++)
                    {
                        tccoef[j] = 0;
                    }

                    //menghasilkan tabel
                    ttable[0, 0] = 0;
                    for (i = 1; i < vartotal_SMin + 1; i++)
                    {
                        ttable[0, i] = tccoef[i - 1];
                    }
                    for (i = 1; i < varinit + 1; i++)
                    {
                        ttable[i, 0] = tbcoef[i - 1, 0];
                    }
                    for (i = 1; i < varinit + 1; i++)
                    {
                        for (j = 1; j < vartotal_SMin + 1; j++)
                        {
                            ttable[i, j] = tcoef[i - 1, j - 1];

                        }

                    }
                    //tabel awal
                    for (i = 0; i < varinit + 1; i++)
                    {
                        for (j = 0; j < vartotal_SMin + 1; j++)
                        {
                            Console.Write(ttable[i, j]);
                        }
                        Console.WriteLine(" hasil");
                    }

                    Mesin.CariMax(ttable);
                    //hasil
                    for (i = 0; i < varinit + 1; i++)
                    {
                        for (j = 0; j < vartotal_SMin + 1; j++)
                            Console.Write("{0,5}  ", ttable[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", ttable[0, 0]);
                }
                     * */
                #endregion

                #region MMax
                else
                {
                    Console.WriteLine("d");
                    double Mval;
                    Mval = 0;

                    //count s1,s2,A1
                    VarSurplus = listDG1min.Count + +listDG1add.Count + listDG2min.Count + listDG2add.Count;
                    VarArtfS = listDG1min.Count + +listDG1add.Count + listDG2min.Count + listDG2add.Count;
                    VarSlack = listDG1max.Count + +listDG1add.Count + listDG2max.Count + listDG2add.Count;
                    VarArtf = listDG1eq.Count + listDG2eq.Count;

                    //tabel awal
                    int VarAll, VarAllDG1, MtableRow, MtableCol;
                    VarAll = VarSurplus + VarSlack + VarArtfS + VarArtf;
                    VarAllDG1 = DG1max + DG1min + DG1eq + (2 * DG1add);
                    MtableRow = constraint + VarAllDG1 + listDG2add.Count;
                    MtableCol = varinit + VarAll;

                    double[,] Mcoef = new double[MtableRow, MtableCol];
                    double[,] Mbcoef = new double[MtableRow, 1];
                    double[] Mccoef = new double[MtableCol];
                    double[,] Mtable = new double[MtableRow + 1, MtableCol + 1];

                    //var basis non basis
                    ArrBasisVar = new string[MtableRow];
                    ArrNonBasisVar = new string[MtableCol];

                    //mariii diisi~~

                    //koefisien fungsi tujuan
                    for (i = 0; i < varinit; i++)
                    {
                        Mccoef[i] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                        ArrNonBasisVar[i] = dataGridView1.Rows[i].Cells[0].Value + "";
                    }
                    //nilai M
                    for (i = 0; i < dataGridView1.RowCount - 1; i++)
                    {
                        for (j = 1; j < dataGridView1.ColumnCount; j++)
                        {
                            if (Convert.ToString(dataGridView1.Rows[i].Cells[j].Value) != string.Empty)
                            {
                                if (Mval < double.Parse(dataGridView1.Rows[i].Cells[j].Value + ""))
                                {
                                    Mval = double.Parse(dataGridView1.Rows[i].Cells[j].Value + "");
                                }
                            }
                        }
                    }
                    for (i = 0; i < dataGridView2.RowCount - 1; i++)
                    {
                        for (j = 1; j < dataGridView2.ColumnCount; j++)
                        {
                            if (Convert.ToString(dataGridView2.Rows[i].Cells[j].Value) != string.Empty)
                            {
                                if (Mval < double.Parse(dataGridView2.Rows[i].Cells[j].Value + ""))
                                {
                                    Mval = double.Parse(dataGridView2.Rows[i].Cells[j].Value + "");
                                }
                            }
                        }
                    }
                    for (i = 0; i < dataGridView4.RowCount - 1; i++)
                    {
                        for (j = 1; j < dataGridView4.ColumnCount; j++)
                        {
                            if (Convert.ToString(dataGridView4.Rows[i].Cells[j].Value) != string.Empty)
                            {
                                if (Mval < double.Parse(dataGridView4.Rows[i].Cells[j].Value + ""))
                                {
                                    Mval = double.Parse(dataGridView4.Rows[i].Cells[j].Value + "");
                                }
                            }
                        }
                    }
                    Mval = -3 * Mval;
                    Console.Write("var M : ");
                    Console.WriteLine(Mval);
                    //max
                    int counter = 0;
                    int max = 0;

                    for (i = 0; i < listDG2max.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = double.Parse(dataGridView4.Rows[listDG2max.ElementAt(i)].Cells[j + 1].Value + "");
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, varinit + max] = 1;
                        Mbcoef[counter, 0] = double.Parse(dataGridView2.Rows[listDG2max.ElementAt(i)].Cells[2].Value + "");

                        ArrBasisVar[counter] = "Slack" + (1 + max + "");
                        ArrNonBasisVar[varinit + max] = "Slack" + (1 + max + "");
                        max++;
                        counter++;
                    }

                    for (i = 0; i < listDG2add.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = double.Parse(dataGridView4.Rows[listDG2add.ElementAt(i)].Cells[j + 1].Value + "");
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, varinit + max] = 1;
                        ArrBasisVar[counter] = "Slack" + (1 + max + "");
                        ArrNonBasisVar[varinit + max] = "Slack" + (1 + max + "");
                        Mbcoef[counter, 0] = double.Parse(dataGridView2.Rows[listDG2add.ElementAt(i)].Cells[2].Value + "");
                        max++;
                        counter++;
                    }

                    for (i = 0; i < listDG1max.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        ArrBasisVar[counter] = "Slack" + (1 + max + "");
                        ArrNonBasisVar[varinit + max] = "Slack" + (1 + max + "");

                        Mcoef[counter, listDG1max.ElementAt(i)] = 1;
                        Mcoef[counter, varinit + max] = 1;
                        max++;
                        Mbcoef[counter, 0] = double.Parse(dataGridView1.Rows[listDG1max.ElementAt(i)].Cells[3].Value + "");
                        counter++;
                    }

                    for (i = 0; i < listDG1add.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        ArrBasisVar[counter] = "Slack" + (1 + max + "");
                        ArrNonBasisVar[varinit + max] = "Slack" + (1 + max + "");

                        Mcoef[counter, listDG1add.ElementAt(i)] = 1;
                        Mcoef[counter, varinit + max] = 1;
                        max++;
                        Mbcoef[counter, 0] = double.Parse(dataGridView1.Rows[listDG1add.ElementAt(i)].Cells[3].Value + "");
                        counter++;
                    }

                    //min
                    int ar1 = 0;
                    int ar2 = 1;
                    int sp = 1;
                    for (i = 0; i < listDG2min.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = double.Parse(dataGridView4.Rows[listDG2min.ElementAt(i)].Cells[j + 1].Value + "");
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, varinit + max + ar1] = -1;
                        ArrNonBasisVar[varinit + max + ar1] = "Surplus" + (sp + "");
                        Mccoef[varinit + max + ar1] = 0;
                        Mcoef[counter, varinit + max + ar2] = 1;
                        ArrNonBasisVar[varinit + max + ar2] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mccoef[varinit + max + ar2] = Mval;
                        Mbcoef[counter, 0] = double.Parse(dataGridView2.Rows[listDG2min.ElementAt(i)].Cells[1].Value + "");

                        sp++;
                        ar1 += 2;
                        ar2 += 2;
                        counter++;

                    }

                    for (i = 0; i < listDG2add.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = double.Parse(dataGridView4.Rows[listDG2add.ElementAt(i)].Cells[j + 1].Value + "");
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, varinit + max + ar1] = -1;
                        ArrNonBasisVar[varinit + max + ar1] = "Surplus" + (sp + "");
                        Mccoef[varinit + max + ar1] = 0;
                        Mcoef[counter, varinit + max + ar2] = 1;
                        ArrNonBasisVar[varinit + max + ar2] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mccoef[varinit + max + ar2] = Mval;
                        ar1 += 2;
                        ar2 += 2;
                        Mbcoef[counter, 0] = double.Parse(dataGridView2.Rows[listDG2add.ElementAt(i)].Cells[1].Value + "");
                        counter++;
                        sp++;
                    }

                    for (i = 0; i < listDG1min.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, listDG1min.ElementAt(i)] = 1;
                        ArrNonBasisVar[varinit + max + ar1] = "Surplus" + (sp + "");
                        Mbcoef[counter, 0] = double.Parse(dataGridView1.Rows[listDG1min.ElementAt(i)].Cells[2].Value + "");
                        Mcoef[counter, varinit + max + ar1] = -1;
                        Mccoef[varinit + max + ar1] = 0;
                        Mcoef[counter, varinit + max + ar2] = 1;
                        ArrNonBasisVar[varinit + max + ar2] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mccoef[varinit + max + ar2] = Mval;
                        sp++;
                        ar1 += 2;
                        ar2 += 2;
                        counter++;
                    }
                    for (i = 0; i < listDG1add.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, listDG1add.ElementAt(i)] = 1;
                        Mbcoef[counter, 0] = double.Parse(dataGridView1.Rows[listDG1add.ElementAt(i)].Cells[2].Value + "");
                        Mcoef[counter, varinit + max + ar1] = -1;
                        ArrNonBasisVar[varinit + max + ar1] = "Surplus" + (sp + "");
                        Mccoef[varinit + max + ar1] = 0;
                        Mcoef[counter, varinit + max + ar2] = 1;
                        ArrNonBasisVar[varinit + max + ar2] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mccoef[varinit + max + ar2] = Mval;
                        sp++;
                        ar1 += 2;
                        ar2 += 2;
                        counter++;
                    }

                    //eq
                    int eq = 0;
                    for (i = 0; i < listDG2eq.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = double.Parse(dataGridView4.Rows[listDG2eq.ElementAt(i)].Cells[j + 1].Value + "");
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, MtableCol - listDG2eq.Count - DG1eq + eq] = 1;
                        Mccoef[MtableCol - listDG2eq.Count - DG1eq + eq] = Mval;
                        ArrNonBasisVar[MtableCol - listDG2eq.Count - DG1eq + eq] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mbcoef[counter, 0] = double.Parse(dataGridView2.Rows[listDG2eq.ElementAt(i)].Cells[1].Value + "");
                        counter++;
                        eq++;
                        sp++;
                    }

                    for (i = 0; i < listDG1eq.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, listDG1eq.ElementAt(i)] = 1;
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, MtableCol - listDG2eq.Count - DG1eq + eq] = 1;
                        Mccoef[MtableCol - listDG2eq.Count - DG1eq + eq] = Mval;
                        ArrNonBasisVar[MtableCol - listDG2eq.Count - DG1eq + eq] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mbcoef[counter, 0] = double.Parse(dataGridView1.Rows[listDG1eq.ElementAt(i)].Cells[2].Value + "");
                        counter++;
                        eq++;
                        sp++;
                    }

                    //mccoef
                    Console.WriteLine("nilai Mccoef: ");
                    for (i = 0; i < MtableCol; i++)
                    {
                        Console.Write(Mccoef[i]);
                        Console.WriteLine(" -- " + ArrNonBasisVar[i]);
                    }
                    Console.WriteLine("nilai Mcoef: ");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine("");
                    }
                    Console.WriteLine("nilai Mbcoef: ");
                    for (i = 0; i < MtableRow; i++)
                    {
                        Console.Write(Mbcoef[i, 0]);
                        Console.WriteLine(" -- " + ArrBasisVar[i]);
                    }
                    //gabungkan menjadi Mtable
                    Mtable[0, 0] = 0;
                    for (i = 1; i < Mccoef.Length + 1; i++)
                    {
                        Mtable[0, i] = Mccoef[i - 1];
                    }
                    for (i = 1; i < Mbcoef.Length + 1; i++)
                    {
                        Mtable[i, 0] = Mbcoef[i - 1, 0];
                    }
                    for (i = 1; i < Mbcoef.Length + 1; i++)
                    {
                        for (j = 1; j < Mccoef.Length + 1; j++)
                        {
                            Mtable[i, j] = Mcoef[i - 1, j - 1];
                        }
                    }
                    Console.WriteLine("mtable: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                        }
                        Console.WriteLine("");
                    }
                    Mesin.InnerProduct(ref Mtable, VarArtfS, VarSlack, VarArtf, Mval);
                    Console.WriteLine("mtable baru: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                            Console.Write(" - ");
                        }
                        Console.WriteLine();
                    }
                    Mesin.CariMax(Mtable);
                    Console.WriteLine("mtable hasil search min: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                            Console.Write(" ");
                        }
                        Console.WriteLine("");
                    }
                    Console.WriteLine("nilai fungsi objektif: ");
                    Console.Write(Mtable[0, 0]);
                }
                #endregion
            }
            #endregion

            #region Button Minimasi

            if (!toGetMax)
            {
                #region Simplex Min

                if (simMax == constraint_SMax && simMin == 0 && simEq == 0)
                {
                    //matriks awal
                    //dr GDV2
                    ArrBasisVar = new string[constraint_SMax];
                    ArrNonBasisVar = new string[vartotal_SMax];

                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coef[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" awal");
                    }
                    //dr GDV1
                    for (i = constraint; i < constraint_SMax; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coef[i, j] = 0;
                        }
                    }
                    int count2 = 0;
                    for (i = constraint; i < constraint_SMax; i++)
                    {
                        coef[i, listDG1max.ElementAt(count2)] = 1;
                        count2++;

                    }

                    //isi nol
                    for (i = 0; i < constraint_SMax; i++)
                    {
                        for (j = varinit; j < vartotal_SMax; j++)
                        {
                            coef[i, j] = 0;
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" tambahan variabel");
                    }
                    //buat variabel bantu
                    for (i = 0; i < constraint_SMax; i++)
                    {
                        for (j = varinit; j < vartotal_SMax; j++)
                        {
                            coef[i, varinit + i] = 1;
                            Console.Write(coef[i, j]);
                        }
                        Console.WriteLine(" tambah 1");
                    }

                    //nilai kanan jika smua constraint memiliki batas maksimum
                    for (i = 0; i < constraint; i++)
                    {
                        bcoef[i, 0] = double.Parse(dataGridView2.Rows[i].Cells[2].Value + "");
                        //report
                        ArrBasisVar[i] = "Slack" + (1 + i + "");
                    }

                    int count = 0;
                    for (i = constraint; i < constraint_SMax; i++)
                    {
                        bcoef[i, 0] = double.Parse(dataGridView1.Rows[listDG1max.ElementAt(count)].Cells[3].Value + "");
                        count++;
                        //report
                        ArrBasisVar[i] = "Slack" + (1 + i + "");

                    }
                    VarSlack = ArrBasisVar.Count();
                    //koefisien fungsi tujuan
                    for (j = 0; j < varinit; j++)
                    {
                        ccoef[j] = double.Parse(dataGridView1.Rows[j].Cells[1].Value + "");
                        //report
                        ArrNonBasisVar[j] = dataGridView1.Rows[j].Cells[0].Value + "";
                    }
                    int counter = 1;
                    for (j = varinit; j < vartotal_SMax; j++)
                    {
                        ccoef[j] = 0;
                        //report
                        ArrNonBasisVar[j] = "Slack" + (counter + "");
                        counter++;
                    }
                    //menghasilkan tabel
                    table[0, 0] = 0;
                    for (i = 1; i < vartotal_SMax + 1; i++)
                    {
                        table[0, i] = ccoef[i - 1];
                    }
                    for (i = 1; i < constraint_SMax + 1; i++)
                    {
                        table[i, 0] = bcoef[i - 1, 0];
                    }
                    for (i = 1; i < constraint_SMax + 1; i++)
                    {
                        for (j = 1; j < vartotal_SMax + 1; j++)
                        {
                            table[i, j] = coef[i - 1, j - 1];
                        }
                    }
                    //matriks awal
                    for (i = 0; i < constraint_SMax + 1; i++)
                    {
                        for (j = 0; j < vartotal_SMax + 1; j++)
                        {
                            Console.Write(table[i, j]);
                        }
                        Console.WriteLine("tabel");
                    }
                    for (i = 0; i < ArrBasisVar.Count(); i++)
                    {
                        Console.Write(ArrBasisVar[i] + "    ");
                    }
                    Console.WriteLine("");
                    for (i = 0; i < ArrNonBasisVar.Count(); i++)
                    {
                        Console.Write(ArrNonBasisVar[i] + "    ");
                    }
                    Console.WriteLine("");
                    Mesin.CariMax(table);
                    //hasil
                    for (i = 0; i < constraint_SMax + 1; i++)
                    {
                        for (j = 0; j < vartotal_SMax + 1; j++)
                            Console.Write("{0,5}  ", table[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", table[0, 0]);

                }
                #endregion

                #region Dual Min
                /*
                else if (simMin == constraint && simMax == 0 && simEq == 0)
                {
                    //matriks awal
                    for (i = 0; i < constraint; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            coefd[i, j] = double.Parse(dataGridView4.Rows[i].Cells[j + 1].Value + "");
                            Console.Write(coefd[i, j]);
                        }
                        Console.WriteLine(" matrik awal");
                    }
                    //transpose
                    tklm = coefd.GetLength(0);
                    Console.WriteLine(tklm);
                    tbrs = coefd.GetLength(1);
                    Console.WriteLine(tbrs);

                    //transpose matrikx
                    for (i = 0; i < tklm; i++)
                    {
                        for (j = 0; j < tbrs; j++)
                        {
                            tcoef[j, i] = coefd[i, j];
                        }
                    }
                    Console.WriteLine(" matriks transpose");

                    //tambah coef dg 0
                    for (i = 0; i < varinit; i++)
                        for (j = constraint; j < vartotal; j++)
                        {
                            tcoef[i, j] = 0;

                        }
                    //var s
                    for (i = 0; i < varinit; i++)
                    {
                        for (j = constraint; j < vartotal; j++)
                        {
                            tcoef[i, constraint + i] = 1;
                        }
                    }

                    //matriks kendala
                    for (i = 0; i < varinit; i++)
                    {
                        for (j = 0; j < vartotal; j++)
                        {
                            Console.Write(tcoef[i, j]);
                        }
                        Console.WriteLine();
                    }
                    //nilai kanan diambil dr fungsi kendala
                    for (i = 0; i < varinit; i++)
                    {
                        tbcoef[i, 0] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                    }

                    //koefisien fungsi tujuan
                    for (j = 0; j < constraint; j++)
                    {
                        tccoef[j] = double.Parse(dataGridView2.Rows[j].Cells[1].Value + "");
                    }
                    for (j = constraint; j < vartotal; j++)
                    {
                        tccoef[j] = 0;
                    }

                    //menghasilkan tabel
                    ttable[0, 0] = 0;
                    for (i = 1; i < vartotal + 1; i++)
                    {
                        ttable[0, i] = tccoef[i - 1];
                    }
                    for (i = 1; i < varinit + 1; i++)
                    {
                        ttable[i, 0] = tbcoef[i - 1, 0];
                    }
                    for (i = 1; i < varinit + 1; i++)
                    {
                        for (j = 1; j < vartotal + 1; j++)
                        {
                            ttable[i, j] = tcoef[i - 1, j - 1];

                        }

                    }
                    //tabel awal
                    for (i = 0; i < varinit + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                        {
                            Console.Write(ttable[i, j]);
                        }
                        Console.WriteLine(" hasil");
                    }

                    Mesin.CariMin(ttable);
                    //hasil
                    for (i = 0; i < varinit + 1; i++)
                    {
                        for (j = 0; j < vartotal + 1; j++)
                            Console.Write("{0,5}  ", ttable[i, j].ToString());
                        Console.WriteLine();
                    }
                    Console.WriteLine("{0} nilai fungsi objective", -ttable[0, 0]);

                }
*/
                #endregion

                #region MMin

                else
                {
                    double Mval;
                    Mval = 0;


                    //count s1,s2,A1
                    VarSurplus = listDG1min.Count + +listDG1add.Count + listDG2min.Count + listDG2add.Count;
                    VarArtfS = listDG1min.Count + +listDG1add.Count + listDG2min.Count + listDG2add.Count;
                    VarSlack = listDG1max.Count + +listDG1add.Count + listDG2max.Count + listDG2add.Count;
                    VarArtf = listDG1eq.Count + listDG2eq.Count;

                    //tabel awal
                    int VarAll, VarAllDG1, MtableRow, MtableCol;
                    VarAll = VarSurplus + VarSlack + VarArtfS + VarArtf;
                    VarAllDG1 = DG1max + DG1min + DG1eq + (2 * DG1add);
                    MtableRow = constraint + VarAllDG1 + listDG2add.Count;
                    MtableCol = varinit + VarAll;

                    double[,] Mcoef = new double[MtableRow, MtableCol];
                    double[,] Mbcoef = new double[MtableRow, 1];
                    double[] Mccoef = new double[MtableCol];
                    double[,] Mtable = new double[MtableRow + 1, MtableCol + 1];

                    //var basis non basis
                    ArrBasisVar = new string[MtableRow];
                    ArrNonBasisVar = new string[MtableCol];

                    //mariii diisi~~

                    //koefisien fungsi tujuan
                    for (i = 0; i < varinit; i++)
                    {
                        Mccoef[i] = double.Parse(dataGridView1.Rows[i].Cells[1].Value + "");
                        ArrNonBasisVar[i] = dataGridView1.Rows[i].Cells[0].Value + "";
                    }
                    //nilai M
                    for (i = 0; i < dataGridView1.RowCount - 1; i++)
                    {
                        for (j = 1; j < dataGridView1.ColumnCount; j++)
                        {
                            if (Convert.ToString(dataGridView1.Rows[i].Cells[j].Value) != string.Empty)
                            {
                                if (Mval < double.Parse(dataGridView1.Rows[i].Cells[j].Value + ""))
                                {
                                    Mval = double.Parse(dataGridView1.Rows[i].Cells[j].Value + "");
                                }
                            }
                        }
                    }
                    for (i = 0; i < dataGridView2.RowCount - 1; i++)
                    {
                        for (j = 1; j < dataGridView2.ColumnCount; j++)
                        {
                            if (Convert.ToString(dataGridView2.Rows[i].Cells[j].Value) != string.Empty)
                            {
                                if (Mval < double.Parse(dataGridView2.Rows[i].Cells[j].Value + ""))
                                {
                                    Mval = double.Parse(dataGridView2.Rows[i].Cells[j].Value + "");
                                }
                            }
                        }
                    }
                    for (i = 0; i < dataGridView4.RowCount - 1; i++)
                    {
                        for (j = 1; j < dataGridView4.ColumnCount; j++)
                        {
                            if (Convert.ToString(dataGridView4.Rows[i].Cells[j].Value) != string.Empty)
                            {
                                if (Mval < double.Parse(dataGridView4.Rows[i].Cells[j].Value + ""))
                                {
                                    Mval = double.Parse(dataGridView4.Rows[i].Cells[j].Value + "");
                                }
                            }
                        }
                    }
                    Mval = 3 * Mval;

                    //max
                    int counter = 0;
                    int max = 0;

                    for (i = 0; i < listDG2max.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = double.Parse(dataGridView4.Rows[listDG2max.ElementAt(i)].Cells[j + 1].Value + "");
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, varinit + max] = 1;
                        Mbcoef[counter, 0] = double.Parse(dataGridView2.Rows[listDG2max.ElementAt(i)].Cells[2].Value + "");

                        ArrBasisVar[counter] = "Slack" + (1 + max + "");
                        ArrNonBasisVar[varinit + max] = "Slack" + (1 + max + "");
                        max++;
                        counter++;
                    }

                    for (i = 0; i < listDG2add.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = double.Parse(dataGridView4.Rows[listDG2add.ElementAt(i)].Cells[j + 1].Value + "");
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, varinit + max] = 1;
                        ArrBasisVar[counter] = "Slack" + (1 + max + "");
                        ArrNonBasisVar[varinit + max] = "Slack" + (1 + max + "");
                        Mbcoef[counter, 0] = double.Parse(dataGridView2.Rows[listDG2add.ElementAt(i)].Cells[2].Value + "");
                        max++;
                        counter++;
                    }

                    for (i = 0; i < listDG1max.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        ArrBasisVar[counter] = "Slack" + (1 + max + "");
                        ArrNonBasisVar[varinit + max] = "Slack" + (1 + max + "");

                        Mcoef[counter, listDG1max.ElementAt(i)] = 1;
                        Mcoef[counter, varinit + max] = 1;
                        max++;
                        Mbcoef[counter, 0] = double.Parse(dataGridView1.Rows[listDG1max.ElementAt(i)].Cells[3].Value + "");
                        counter++;
                    }

                    for (i = 0; i < listDG1add.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        ArrBasisVar[counter] = "Slack" + (1 + max + "");
                        ArrNonBasisVar[varinit + max] = "Slack" + (1 + max + "");

                        Mcoef[counter, listDG1add.ElementAt(i)] = 1;
                        Mcoef[counter, varinit + max] = 1;
                        max++;
                        Mbcoef[counter, 0] = double.Parse(dataGridView1.Rows[listDG1add.ElementAt(i)].Cells[3].Value + "");
                        counter++;
                    }

                    //min
                    Console.WriteLine(" DG2 MIn: " + listDG2min.Count);
                    int ar1 = 0;
                    int ar2 = 1;
                    int sp = 1;
                    for (i = 0; i < listDG2min.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = double.Parse(dataGridView4.Rows[listDG2min.ElementAt(i)].Cells[j + 1].Value + "");

                        }

                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;

                        }

                        Mcoef[counter, varinit + max + ar1] = -1;
                        ArrNonBasisVar[varinit + max + ar1] = "Surplus" + (sp + "");
                        Mccoef[varinit + max + ar1] = 0;
                        Mcoef[counter, varinit + max + ar2] = 1;
                        ArrNonBasisVar[varinit + max + ar2] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mccoef[varinit + max + ar2] = Mval;
                        Mbcoef[counter, 0] = double.Parse(dataGridView2.Rows[listDG2min.ElementAt(i)].Cells[1].Value + "");

                        sp++;
                        ar1 += 2;
                        ar2 += 2;
                        counter++;
                    }

                    for (i = 0; i < listDG2add.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = double.Parse(dataGridView4.Rows[listDG2add.ElementAt(i)].Cells[j + 1].Value + "");
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, varinit + max + ar1] = -1;
                        ArrNonBasisVar[varinit + max + ar1] = "Surplus" + (sp + "");
                        Mccoef[varinit + max + ar1] = 0;
                        Mcoef[counter, varinit + max + ar2] = 1;
                        ArrNonBasisVar[varinit + max + ar2] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mccoef[varinit + max + ar2] = Mval;
                        ar1 += 2;
                        ar2 += 2;
                        Mbcoef[counter, 0] = double.Parse(dataGridView2.Rows[listDG2add.ElementAt(i)].Cells[1].Value + "");
                        counter++;
                        sp++;
                    }

                    for (i = 0; i < listDG1min.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, listDG1min.ElementAt(i)] = 1;
                        ArrNonBasisVar[varinit + max + ar1] = "Surplus" + (sp + "");
                        Mbcoef[counter, 0] = double.Parse(dataGridView1.Rows[listDG1min.ElementAt(i)].Cells[2].Value + "");
                        Mcoef[counter, varinit + max + ar1] = -1;
                        Mccoef[varinit + max + ar1] = 0;
                        Mcoef[counter, varinit + max + ar2] = 1;
                        ArrNonBasisVar[varinit + max + ar2] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mccoef[varinit + max + ar2] = Mval;
                        sp++;
                        ar1 += 2;
                        ar2 += 2;
                        counter++;
                    }

                    for (i = 0; i < listDG1add.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, listDG1add.ElementAt(i)] = 1;
                        Mbcoef[counter, 0] = double.Parse(dataGridView1.Rows[listDG1add.ElementAt(i)].Cells[2].Value + "");
                        Mcoef[counter, varinit + max + ar1] = -1;
                        ArrNonBasisVar[varinit + max + ar1] = "Surplus" + (sp + "");
                        Mccoef[varinit + max + ar1] = 0;
                        Mcoef[counter, varinit + max + ar2] = 1;
                        ArrNonBasisVar[varinit + max + ar2] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mccoef[varinit + max + ar2] = Mval;
                        sp++;
                        ar1 += 2;
                        ar2 += 2;
                        counter++;
                    }

                    //eq
                    int eq = 0;
                    for (i = 0; i < listDG2eq.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = double.Parse(dataGridView4.Rows[listDG2eq.ElementAt(i)].Cells[j + 1].Value + "");
                        }
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, MtableCol - listDG2eq.Count - DG1eq + eq] = 1;
                        Mccoef[MtableCol - listDG2eq.Count - DG1eq + eq] = Mval;
                        ArrNonBasisVar[MtableCol - listDG2eq.Count - DG1eq + eq] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mbcoef[counter, 0] = double.Parse(dataGridView2.Rows[listDG2eq.ElementAt(i)].Cells[1].Value + "");
                        counter++;
                        eq++;
                        sp++;
                    }

                    for (i = 0; i < listDG1eq.Count; i++)
                    {
                        for (j = 0; j < varinit; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, listDG1eq.ElementAt(i)] = 1;
                        for (j = varinit; j < MtableCol; j++)
                        {
                            Mcoef[counter, j] = 0;
                        }
                        Mcoef[counter, MtableCol - listDG2eq.Count - DG1eq + eq] = 1;
                        Mccoef[MtableCol - listDG2eq.Count - DG1eq + eq] = Mval;
                        ArrNonBasisVar[MtableCol - listDG2eq.Count - DG1eq + eq] = "Artificial" + (sp + "");
                        ArrBasisVar[counter] = "Artificial" + (sp + "");
                        Mbcoef[counter, 0] = double.Parse(dataGridView1.Rows[listDG1eq.ElementAt(i)].Cells[2].Value + "");
                        counter++;
                        eq++;
                        sp++;
                    }

                    //mccoef
                    Console.WriteLine("nilai Mccoef: ");
                    for (i = 0; i < MtableCol; i++)
                    {
                        Console.Write(Mccoef[i]);
                        Console.WriteLine(" -- " + ArrNonBasisVar[i]);
                    }
                    Console.WriteLine("nilai Mcoef: ");
                    for (i = 0; i < MtableRow; i++)
                    {
                        for (j = 0; j < MtableCol; j++)
                        {
                            Console.Write(Mcoef[i, j]);
                        }
                        Console.WriteLine("");
                    }
                    Console.WriteLine("nilai Mbcoef: ");
                    for (i = 0; i < MtableRow; i++)
                    {
                        Console.Write(Mbcoef[i, 0]);
                        Console.WriteLine(" -- " + ArrBasisVar[i]);
                    }
                    //gabungkan menjadi Mtable
                    Mtable[0, 0] = 0;
                    for (i = 1; i < Mccoef.Length + 1; i++)
                    {
                        Mtable[0, i] = Mccoef[i - 1];
                    }
                    for (i = 1; i < Mbcoef.Length + 1; i++)
                    {
                        Mtable[i, 0] = Mbcoef[i - 1, 0];
                    }
                    for (i = 1; i < Mbcoef.Length + 1; i++)
                    {
                        for (j = 1; j < Mccoef.Length + 1; j++)
                        {
                            Mtable[i, j] = Mcoef[i - 1, j - 1];
                        }
                    }
                    Console.WriteLine("mtable: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                        }
                        Console.WriteLine("");
                    }
                    Mesin.InnerProduct(ref Mtable, VarArtfS, VarSlack, VarArtf, Mval);
                    Console.WriteLine("mtable baru: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                            Console.Write(" - ");
                        }
                        Console.WriteLine();
                    }
                    Mesin.CariMin(Mtable);
                    Console.WriteLine("mtable hasil search min: ");
                    //tampilkan mtable
                    for (i = 0; i < MtableRow + 1; i++)
                    {
                        for (j = 0; j < MtableCol + 1; j++)
                        {
                            Console.Write(Mtable[i, j]);
                            Console.Write(" ");
                        }
                        Console.WriteLine("");
                    }
                    Console.WriteLine("nilai fungsi objektif: ");
                    Console.Write(Mtable[0, 0]);
                }
                #endregion
            }

            #endregion

        }

        //sensitivity analysis
        public static string[] Var_Analysis_Max;
        public static string[] Var_Analysis_Min;
        public static double[] Var_Dss_Value;
        public static double[] SS_Dss_Value;
        public static double[] Var_Dss_print;

        public static void Variable_Analysis()
        {
            int i, j;
            double s;
            List<double> ListMin = new List<double>();
            List<double> ListMax = new List<double>();
            int baris = ListRepValue.ElementAt(0).GetLength(0) - 1;
            int optB = ListBasisVar.Count;
            int optT = ListRepValue.Count;

            Var_Analysis_Max = new string[baris];
            Var_Analysis_Min = new string[baris];
            Var_Dss_Value = new double[baris];
            Var_Dss_print = new double[baris];
            SS_Dss_Value = new double[baris];//huuuuuu

            #region basis

            //untuk var keputusan yg mjd basis
            for (int x = 0; x < baris; x++)
            {
                for (int y = 0; y < baris; y++)
                {
                    //jika sama
                    if (ListBasisVar.ElementAt(optB - 1)[x] == ArrNonBasisVar[y])
                    {
                        i = x;
                        j = y;
                        for (int a = 0; a < ListRepValue.ElementAt(0).GetLength(1) - 1; a++)
                        {

                            if (ListRepValue.ElementAt(optT - 1)[0, a + 1] != 0)
                            {

                                s = (-ListRepValue.ElementAt(optT - 1)[0, a + 1]) / (-ListRepValue.ElementAt(optT - 1)[i + 1, a + 1]);

                                if (-ListRepValue.ElementAt(optT - 1)[i + 1, a + 1] < 0)
                                {
                                    ListMin.Add(s);
                                }
                                else if (-ListRepValue.ElementAt(optT - 1)[i + 1, a + 1] > 0)
                                {
                                    ListMax.Add(s);
                                }

                            }
                        }

                        Console.WriteLine("List max");
                        for (int w = 0; w < ListMax.Count(); w++)
                            Console.WriteLine(ListMax[w]);

                        Console.WriteLine("List min");
                        for (int w = 0; w < ListMin.Count(); w++)
                            Console.WriteLine(ListMin[w]);

                        if (ListMax.Count != 0)
                        {
                            Var_Analysis_Max[j] = Convert.ToString(ListMax.Min());
                        }
                        else
                        {
                            Var_Analysis_Max[j] = "Infinity";
                        }
                        if (ListMin.Count != 0)
                        {
                            Var_Analysis_Min[j] = Convert.ToString(ListMin.Max());
                        }
                        else
                        {
                            Var_Analysis_Min[j] = "Infinity";
                        }
                        Console.WriteLine("baris :" + i);
                        Console.WriteLine("kolom :" + j);
                        ListMax.Clear();
                        ListMin.Clear();
                    }
                }
            }
            //isi utk value primal
            for (i = 0; i < baris; i++)
            {
                Var_Dss_Value[i] = 0;

            }
            //int count = 0;
            for (int x = 0; x < baris; x++)
            {
                for (int y = 0; y < baris; y++)
                {
                    //jika sama
                    if (ListBasisVar.ElementAt(optB - 1)[x] == ArrNonBasisVar[y])
                    {
                        i = x;
                        j = y;
                        Var_Dss_Value[j] = ListRepValue.ElementAt(optT - 1)[i + 1, 0];

                    }
                    if (ListBasisVar.ElementAt(optB - 1)[x] != ArrNonBasisVar[y])
                    {

                    }
                }
            }

            //copi
            for (i = 0; i < Var_Dss_Value.Count(); i++)
            {
                Var_Dss_print[i] = Var_Dss_Value[i];
            }
            #endregion

            #region nonbasis

            for (int x = 0; x < baris; x++)
            {
                if (ListRepValue.ElementAt(optT - 1)[0, x + 1] != 0)
                {
                    s = -ListRepValue.ElementAt(optT - 1)[0, x + 1];
                    Var_Analysis_Max[x] = Convert.ToString(s);
                    Var_Analysis_Min[x] = "Infinity";
                }
            }
            #endregion
        }
        public static string[] Const_Analysis_Max;
        public static string[] Const_Analysis_Min;
        public static double[] Const_Dss_Val;
        public static string[] NonBasis;
        public static void Constraint_Analysis()
        {
            int i, j; int sl = 1, a = 1;
            int jml = VarArtf + VarSlack + VarArtfS;
            int start = ListRepValue.ElementAt(0).GetLength(1) - VarSurplus - jml;
            double[,] bantu = new double[jml + 1, jml];
            NonBasis = new string[jml];

            for (i = 0; i < VarSlack; i++)
            {
                NonBasis[i] = "Slack" + (sl + "");
                sl++;
            }
            for (i = VarSlack; i < jml; i++)
            {
                NonBasis[i] = "Artificial" + (a + "");
                a++;
            }
            for (i = 0; i < jml; i++)
            {
                bantu[0, i] = ListRepValue.ElementAt(ListRepValue.Count - 1)[0, start + 1];
            }
            for (i = 1; i < jml + 1; i++)
            {
                int counter = 1;

                for (j = 0; j < VarSlack; j++)
                {
                    bantu[i, j] = ListRepValue.ElementAt(ListRepValue.Count - 1)[i, start + j];
                    sl++;
                }
                for (j = VarSlack; j < VarSlack + VarArtfS; j++)
                {
                    bantu[i, j] = ListRepValue.ElementAt(ListRepValue.Count - 1)[i, start + j + counter];
                    counter++;
                }
                for (j = VarSlack + VarArtfS; j < jml; j++)
                {
                    bantu[i, j] = ListRepValue.ElementAt(ListRepValue.Count - 1)[i, start + VarSurplus + j];
                }
            }
            Console.WriteLine("matrikx bantunyaa: ");
            for (i = 0; i < jml + 1; i++)
            {
                for (j = 0; j < jml; j++)
                {
                    Console.Write(bantu[i, j]);
                }
                Console.WriteLine();
            }

            Const_Analysis_Max = new string[jml];
            Const_Analysis_Min = new string[jml];
            Const_Dss_Val = new double[jml];
            List<double> list_max = new List<double>();
            List<double> list_min = new List<double>();

            #region basis
            Console.WriteLine("NonBasis: ");
            for (i = 0; i < jml; i++)
            {
                Console.WriteLine(NonBasis[i] + "--");

            }
            for (i = 0; i < jml; i++)
            {
                for (j = 0; j < jml; j++)
                {
                    if (ListBasisVar.ElementAt(ListBasisVar.Count - 1)[i] == NonBasis[j])
                    {
                        Const_Analysis_Min[j] = Convert.ToString(-(ListRepValue.ElementAt(ListRepValue.Count - 1)[i + 1, 0]));
                        Console.WriteLine(Const_Analysis_Min[j]);
                        Const_Analysis_Max[j] = "Infinity";
                        Const_Dss_Val[j] = ListRepValue.ElementAt(ListRepValue.Count - 1)[i + 1, 0];
                        Console.WriteLine(Const_Analysis_Max[j]);
                    }
                }
            }
            #endregion
            #region nonbasis
            double S;
            for (j = 0; j < jml; j++)
            {

                if (bantu[0, j] != 0)
                {
                    for (i = 1; i < jml + 1; i++)
                    {
                        Console.WriteLine("pembagi: " + (-ListRepValue.ElementAt(ListRepValue.Count - 1)[i, 0]));
                        Console.WriteLine("dibagi: " + (-bantu[i, j]));
                        S = (-ListRepValue.ElementAt(ListRepValue.Count - 1)[i, 0]) / (bantu[i, j]);
                        Console.WriteLine("hasil: " + S);
                        if (bantu[i, j] < 0)
                        {
                            list_max.Add(S);
                        }
                        else if (bantu[i, j] > 0)
                        {
                            list_min.Add(S);
                        }
                    }
                    for (i = 0; i < list_max.Count; i++)
                    {
                        Console.WriteLine("ListConstMax: " + list_max.ElementAt(i));
                    }
                    for (i = 0; i < list_min.Count; i++)
                    {
                        Console.WriteLine("ListConstMin: " + list_min.ElementAt(i));
                    }
                    //add ke array
                    if (list_max.Count != 0)
                    {
                        Const_Analysis_Max[j] = Convert.ToString(list_max.Min());
                    }
                    else
                    {
                        Const_Analysis_Max[j] = "Infinity";
                    }
                    if (list_min.Count != 0)
                    {
                        Const_Analysis_Min[j] = Convert.ToString(list_min.Max());
                    }
                    else
                    {
                        Const_Analysis_Min[j] = "Infinity";
                    }
                    list_max.Clear();
                    list_min.Clear();
                }
            }
            #endregion
            Console.WriteLine("ConstMin: ");
            for (i = 0; i < jml; i++)
            {
                Console.WriteLine(Const_Analysis_Min[i] + "");
            }
            Console.WriteLine("ConstMax :");
            for (i = 0; i < jml; i++)
            {
                Console.WriteLine(Const_Analysis_Max[i] + "");
            }
        }

    }

}

